< previous page page_137 next page >

Page 137
Dynamic Arrays
Many times you wont know the number of elements you need in an array until runtime. If this happens, you can create an array with no elements and then redimension that array at runtime to a particular size. Lets create a dynamic module-level array by using the following code:
9f31265bb40b592b1929e18a47ab28e5.gif
Dim maboolBonus() As Boolean
The parentheses with no number between them specifies that this is an array, but it has yet to be given the number of elements. Now, when you click the Dynamic command button, youll want to redimension the array to 3, which will create an array that can handle four elements:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdDynamic_Click()
   Dim intSize As Integer

   intSize = 3

   ' Redimension the array to the new size
   ReDim maboolBonus(intSize) As Boolean

   maboolBonus(0) = True
   maboolBonus(1) = True
   maboolBonus(2) = False
   maboolBonus(3) = False

   ' Print Bonus Flags
   Me.Cls
   Me.Print maboolBonus(0)
   Me.Print maboolBonus(1)
   Me.Print maboolBonus(2)
   Me.Print maboolBonus(3)
End Sub
When you use the ReDim statement, Visual Basic completely erases the array and redimensions it to the new size you specify. Notice that you use a variable, intSize, to specify the size of the array. You could ask the user to input that value, and then you could place it in the ReDim statement.

 
< previous page page_137 next page >

If you like this book, buy it!